<template>
  <div v-if="showPage">
    <comment :Posts="posts"></comment>
    <bottoms></bottoms>
  </div>
</template>

<script>
  import comment from '@/components/comment/index'
  import bottoms from '@/components/reachBottom/index'
  export default {
    mpType: 'page',
    components: {
      comment,
      bottoms
    },
    data() {
      return {
        id: "",
        type: "",
        posts:[],
        showPage: true,
        loadingEnd:{
          moduleOne: false,
        },
        totalPage: 1,
        currentPage: 1
      }
    },
    methods:{
      // 影视学堂发帖列表
      getPostsList(){
        this.$ajax
          .post(this.$api.postsList,{movie_id:this.id, page_size: 10, page: this.currentPage})
          .then(res => {
            this.posts.push(...res.data);
            this.loadingEnd.moduleOne = true
            this.totalPage = res.total_page
            if(res.total_count === 0){
              this.$store.dispatch("changeIsEmpty", true);
            } else {
              this.$store.dispatch("changeIsEmpty", false);
            }
          })
          .catch(err => {
            this.loadingEnd.moduleOne = true
            uni.showToast({
              title: err,
              icon: 'none',
              duration: 2000
            })
          });
      },
      // 项目发帖列表
      getProjectPostsList(){
        this.$ajax
          .post(this.$api.getProjectPosts,{project_id:this.id, page_size: 10, page: this.currentPage})
          .then(res => {
            this.posts.push(...res.data);
            this.loadingEnd.moduleOne = true
            this.totalPage = res.total_page
            if(res.total_count === 0){
              this.$store.dispatch("changeIsEmpty", true);
            } else {
              this.$store.dispatch("changeIsEmpty", false);
            }
          })
          .catch(err => {
            this.loadingEnd.moduleOne = true
            uni.showToast({
              title: err,
              icon: 'none',
              duration: 2000
            })
          });
      },
      viewLoad(){
        this.posts = [];
        this.$store.dispatch("changeIsMore", false);
        this.$store.dispatch("changeIsEnd", false);
        this.$store.dispatch("changeIsEmpty", false);
        this.currentPage = 1
        this.loadingEnd = {
          moduleOne: false,
        };
        uni.showLoading({
          title: '加载中',
          mask: true
        });
        if(this.type === 'movie'){
          this.getPostsList();
        } else {
          this.getProjectPostsList()
        }
      }
    },
    computed:{
      hideLoading(){
        if(this.loadingEnd.moduleOne){
          setTimeout(function(){
            uni.hideLoading()
            }, 800)
        }
      }
    },
    onPullDownRefresh(){
      this.viewLoad()
      uni.stopPullDownRefresh() //停止下拉刷新
    },
    onReachBottom(){
      if(this.totalPage > this.currentPage){
        this.$store.dispatch("changeIsMore", true);
        this.$store.dispatch("changeIsEnd", false);
        this.currentPage++
        if(this.type === 'movie'){
          this.getPostsList();
        } else {
          this.getProjectPostsList()
        }
      } else {
        this.$store.dispatch("changeIsEnd", true);
        this.$store.dispatch("changeIsMore", false);
      }
    },
    onHide(){
      this.showPage= false;
    },
    onShow(){
      this.showPage= true;
      this.viewLoad()
    },
    onLoad(res){
      this.id = res.id;
      this.type = res.type;

    }
  }
</script>

<style scoped>

</style>
